package com.lonely.fx.bind;

import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-24 13:34
 * @author: ztkj-hzb
 * @description:
 **/
public class 绑定计算案例 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        HBox hBox = new HBox();
        hBox.setStyle("-fx-background-color: pink");

        //设置按钮的宽度
        DoubleBinding widthDivide = anchorPane.widthProperty().divide(2);
        DoubleBinding heightDivide = anchorPane.heightProperty().divide(2);

        anchorPane.getChildren().addAll(hBox);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

        //将hbox的宽高绑定一个表达式，动态计算值
        hBox.prefWidthProperty().bind(widthDivide);
        hBox.prefHeightProperty().bind(heightDivide);


    }

}
